package com.zzyl.service.impl;

import com.google.common.collect.Lists;
import com.google.protobuf.Method;
import com.zzyl.constant.Constants;
import com.zzyl.dto.UserLoginRequestDto;
import com.zzyl.entity.Member;
import com.zzyl.mapper.MemberMapper;
import com.zzyl.properties.JwtProperties;
import com.zzyl.service.MemberService;
import com.zzyl.service.WechatService;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.StringUtils;
import com.zzyl.vo.LoginVo;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Random;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    WechatService wechatService;

    @Autowired
    MemberMapper memberMapper;

    @Autowired
    JwtProperties jwtProperties;

    static List<String> DEFAULT_NICKNAME_PREFIX = Lists.newArrayList(
            "淘宝用户",
            "骑手张**",
            "神人",
            "这辈子有了",
            "不是哥们你别卡我啊",
            "完美用户",
            "Donk666",
            "Something",
            "Zmjjhh"
    );

    /**
     * 小程序登录
     *
     * @param userLoginRequestDto
     * @return
     */
    @Override
    public LoginVo login(@RequestBody UserLoginRequestDto userLoginRequestDto) {
        // 调用WechatService获取openId
        String openId = wechatService.getOpenId(userLoginRequestDto.getCode());

        // 获取手机号
        String phone = wechatService.getPhone(userLoginRequestDto.getPhoneCode());

        // 根据openId查询表中是否有此用户
        Member member = memberMapper.getByOpenId(openId);

        // 注册新用户
        if (ObjectUtil.isEmpty(member)) {
            member = Member.builder()
                    .openId(openId)
                    .phone(phone)
                    .build();
        }

        insertOnUpdate(member,phone);

        HashMap<String, Object> claims = new HashMap<>();
        claims.put(Constants.JWT_USERID,member.getId());
        claims.put(Constants.JWT_USERNAME,member.getName());

        // 生成token
        String jwt = JwtUtil.createJWT(
                jwtProperties.getSecretKey(),
                jwtProperties.getTtl(),
                claims
        );

        LoginVo loginVo = new LoginVo();
        loginVo.setToken(jwt);
        loginVo.setNickName(member.getName());
        return loginVo;
    }

    private void insertOnUpdate(Member member, String phone) {
        // 生成name
        String nikeName = DEFAULT_NICKNAME_PREFIX.get((int) (Math.random() * DEFAULT_NICKNAME_PREFIX.size()))
                + StringUtils.substring(phone,7);

        // 新用户
        if (ObjectUtil.isEmpty(member.getId())) {
            member.setName(nikeName);
            memberMapper.save(member);
            return;
        }

        // 用户更新了手机号
        if (ObjectUtil.notEqual(member.getPhone(),phone)) {
            member.setPhone(phone);
            memberMapper.update(member);
        }
    }
}
